package org.cgs.web.server.admin;

import java.io.IOException;
import java.sql.*;

import javax.servlet.http.*;

import org.apache.log4j.Logger;
import org.cgs.web.database.DatabaseConnector;

public final class DeleteInstructor extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = Logger.getLogger(DeleteInstructor.class);

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        // Get School info from the web page
        String username = request.getParameter("username") == null ? "" : request.getParameter("username");

        // Get ready to make DB call
        Connection con = null;
        PreparedStatement pstmt = null;
        try
        {
            // Get instructor account from DB matching input
            con = DatabaseConnector.getInstance().getConnection();
            pstmt = con.prepareStatement("DELETE FROM instructorAuthorization WHERE netID=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
            pstmt.setString(1, username);
            pstmt.executeUpdate();
        } // End try block
        catch (Exception e)
        {
            LOGGER.error("Exception thrown while atttempting to delete an instructor", e);
        }
        finally
        {
            try
            {
                // Close the PreparedStatement
                if (pstmt != null)
                {
                    pstmt.close();
                }
            }
            catch (Exception e)
            {
                LOGGER.error("Exception cleaning up prepared statement", e);
            }

            try
            {
                // Close the connection
                if (con != null)
                {
                    con.close();
                }
            }
            catch (Exception e)
            {
                LOGGER.error("Exception cleaning up connection to the database", e);
            }
        } // End finally

        response.sendRedirect("admin/viewInstructors.jsp");
    } // End method
} // End class
